package cn.tedu.hospitalgroupproject.server.content.dao.persist.repository.impl;

import cn.tedu.hospitalgroupproject.server.common.pojo.vo.PageData;
import cn.tedu.hospitalgroupproject.server.common.util.PageInfoToPageDataConverter;
import cn.tedu.hospitalgroupproject.server.content.dao.persist.mapper.DoctorMapper;
import cn.tedu.hospitalgroupproject.server.content.dao.persist.repository.IDoctorRepository;
import cn.tedu.hospitalgroupproject.server.content.pojo.entity.Doctor;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DoctorListItemVO;
import cn.tedu.hospitalgroupproject.server.content.pojo.vo.DoctorStandardVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class DoctorRepositoryImpl implements IDoctorRepository {

    @Autowired
    private DoctorMapper doctorMapper;


    public DoctorRepositoryImpl() {
        log.info("创建数据库对象: DoctorRepositoryImpl");
    }

    @Override
    public int insert(Doctor doctor) {
        log.debug("开始向[医生表]中插入数据:{}",doctor);
        return doctorMapper.insert(doctor);
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行[删除指定医生ID数据]:{}",id);
        return doctorMapper.deleteById(id);
    }

    @Override
    public int updateById(Doctor doctor) {
        log.debug("开始执行[更新指定医生ID数据]:{}",doctor);
        return doctorMapper.updateById(doctor);
    }

    @Override
    public int updateIsAdminById(Long id, Integer isAdmin) {
        log.debug("开始执行[更新指定医生ID是否为管理员],医生ID:{},是否为管理员:{}",id,isAdmin);
        Doctor doctor=new Doctor();
        doctor.setIsAdmin(isAdmin);
        QueryWrapper<Doctor> wrapper=new QueryWrapper<>();
        wrapper.eq("id",id);
        return doctorMapper.update(doctor,wrapper);
    }

    @Override
    public int countByClinicId(Long id) {
        log.debug("开始执行[根据指定门诊ID查询对应医生数量]:门诊ID:{}",id);
        return doctorMapper.selectDoctorByClinicId(id).size();
    }

    @Override
    public int countByTitleId(Long id) {
        log.debug("开始执行[根据指定职称ID查询对应医生数量]:职称ID:{}",id);
        return doctorMapper.selectDoctorByTitleId(id).size();

    }

    @Override
    public DoctorStandardVO getStandardById(Long id) {
        log.debug("开始执行[根据指定ID查询对应医生数据]:{}",id);
        return doctorMapper.getStandardById(id);
    }

    @Override
    public PageData<DoctorListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询医生列表】，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<DoctorListItemVO> tagTypeList = doctorMapper.listDoctor();
        PageInfo<DoctorListItemVO> pageInfo = new PageInfo<>(tagTypeList);
        PageData<DoctorListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
